package com.mentalhealthplatform.teacherV2.controller;

import com.mentalhealthplatform.appModels.FamilyMessage;
import com.mentalhealthplatform.appModels.Student;
import com.mentalhealthplatform.teacherV2.dto.FamilyMessageDTO;
import com.mentalhealthplatform.teacherV2.service.FamilyMessageServiceV2;
import com.mentalhealthplatform.teacherV2.service.StudentServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/familymessage")
public class FamilyMessageControllerV2 {

    @Autowired
    private FamilyMessageServiceV2 familyMessageServiceV2;

    @Autowired
    private StudentServiceV2 studentServiceV2;

    // 创建家庭信息
    @PostMapping
    public ResponseEntity<FamilyMessageDTO> createFamilyMessage(@RequestBody FamilyMessageDTO familyMessageDTO) {
        FamilyMessage familyMessage = convertToEntity(familyMessageDTO);
        FamilyMessage savedFamilyMessage = familyMessageServiceV2.saveFamilyMessage(familyMessage, UUID.fromString(familyMessageDTO.getStudentId().toString()));
        return ResponseEntity.ok(convertToDTO(savedFamilyMessage));
    }

    // 根据 ID 获取家庭信息
    @GetMapping("/{id}")
    public ResponseEntity<FamilyMessageDTO> getFamilyMessageById(@PathVariable Long id) {
        FamilyMessage familyMessage = familyMessageServiceV2.getFamilyMessageById(id);
        return familyMessage != null ? ResponseEntity.ok(convertToDTO(familyMessage)) : ResponseEntity.notFound().build();
    }

    // 获取所有家庭信息
    @GetMapping
    public ResponseEntity<List<FamilyMessageDTO>> getAllFamilyMessages() {
        List<FamilyMessage> familyMessages = familyMessageServiceV2.getAllFamilyMessages();
        List<FamilyMessageDTO> familyMessageDTOs = familyMessages.stream().map(this::convertToDTO).collect(Collectors.toList());
        return ResponseEntity.ok(familyMessageDTOs);
    }

    // 根据学生 ID 获取家庭信息
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<FamilyMessageDTO>> getFamilyMessagesByStudentId(@PathVariable UUID studentId) {
        List<FamilyMessage> familyMessages = familyMessageServiceV2.getFamilyMessagesByStudentId(studentId);
        List<FamilyMessageDTO> familyMessageDTOs = familyMessages.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(familyMessageDTOs);
    }

    // 更新家庭信息
    @PutMapping("/{id}")
    public ResponseEntity<FamilyMessageDTO> updateFamilyMessage(@PathVariable Long id, @RequestBody FamilyMessageDTO familyMessageDTO) {
        FamilyMessage familyMessage = convertToEntity(familyMessageDTO);
        familyMessage.setId(id);
        FamilyMessage updatedFamilyMessage = familyMessageServiceV2.updateFamilyMessage(familyMessage);
        return ResponseEntity.ok(convertToDTO(updatedFamilyMessage));
    }

    // 删除家庭信息
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFamilyMessage(@PathVariable Long id) {
        familyMessageServiceV2.deleteFamilyMessage(id);
        return ResponseEntity.noContent().build();
    }

    // 根据关键字搜索家庭信息
    @GetMapping("/search")
    public ResponseEntity<List<FamilyMessageDTO>> searchFamilyMessages(@RequestParam String keyword) {
        List<FamilyMessage> familyMessages = familyMessageServiceV2.searchFamilyMessages(keyword);
        List<FamilyMessageDTO> familyMessageDTOs = familyMessages.stream().map(this::convertToDTO).collect(Collectors.toList());
        return ResponseEntity.ok(familyMessageDTOs);
    }

    // 将 FamilyMessageDTO 转换为 FamilyMessage
    private FamilyMessage convertToEntity(FamilyMessageDTO familyMessageDTO) {
        FamilyMessage familyMessage = new FamilyMessage();
        familyMessage.setId(familyMessageDTO.getId());
        familyMessage.setFatherName(familyMessageDTO.getFatherName());
        familyMessage.setMotherName(familyMessageDTO.getMotherName());
        familyMessage.setFatherPhone(familyMessageDTO.getFatherPhone());
        familyMessage.setMotherPhone(familyMessageDTO.getMotherPhone());
        familyMessage.setHomeAddress(familyMessageDTO.getHomeAddress());

        if (familyMessageDTO.getStudentId() != null) {
            Student student = studentServiceV2.findById(familyMessageDTO.getStudentId())
                    .orElseThrow(() -> new RuntimeException("未找到对应的学生"));
            familyMessage.setStudent(student);
        }

        return familyMessage;
    }

    // 将 FamilyMessage 转换为 FamilyMessageDTO
    private FamilyMessageDTO convertToDTO(FamilyMessage familyMessage) {
        FamilyMessageDTO familyMessageDTO = new FamilyMessageDTO();
        familyMessageDTO.setId(familyMessage.getId());
        familyMessageDTO.setFatherName(familyMessage.getFatherName());
        familyMessageDTO.setMotherName(familyMessage.getMotherName());
        familyMessageDTO.setFatherPhone(familyMessage.getFatherPhone());
        familyMessageDTO.setMotherPhone(familyMessage.getMotherPhone());
        familyMessageDTO.setHomeAddress(familyMessage.getHomeAddress());

        if (familyMessage.getStudent() != null) {
            familyMessageDTO.setStudentId(familyMessage.getStudent().getId());
            familyMessageDTO.setStudentNumber(familyMessage.getStudent().getNumber()); // 设置学生学号
            familyMessageDTO.setStudentName(familyMessage.getStudent().getName()); // 设置学生姓名
        } else {
            familyMessageDTO.setStudentId(null); // 或者根据需要设置默认值
            familyMessageDTO.setStudentNumber(null); // 或者根据需要设置默认值
            familyMessageDTO.setStudentName(null); // 或者根据需要设置默认值
        }

        return familyMessageDTO;
    }
}